﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using Microsoft.SmartDevice.Connectivity;

namespace RemoteToolApp
{
    public partial class ProcessManager : Form
    {
        public Device device;

        public ProcessManager(Device a)
        {
            device = a;
            InitializeComponent();
            ShowProcesses();
        }

        private void ShowProcesses()
        {
            Collection<RemoteProcess> processes = device.GetRunningProcesses();
            ProcessList.Items.Clear();
            foreach (RemoteProcess process in processes)
	        {
                ProcessList.Items.Add(process);    
	        }
   
        }

        private void EndProcessButton_Click(object sender, EventArgs e)
        {
            RemoteProcess process = (RemoteProcess)ProcessList.SelectedItem;
            try
            {
                process.Kill();
                //textBox1.Text.Insert(1, "Killed: " + "\n" + process.FileName + process.GetExitCode().ToString());
                LogText.Text = "(" + System.DateTime.Now.TimeOfDay.ToString() + ") " +  "Killed: " + process.FileName + "  " + process.GetId().ToString() + "  " + process.GetExitCode().ToString() + "\r\n" + LogText.Text;
                this.ShowProcesses();
            }
            catch (Exception ex)
            {
                LogText.Text = "(" + System.DateTime.Now.TimeOfDay.ToString() + ") " +  "Exception:  " + ex.Message + "\r\n" + LogText.Text;
            }

        }

        private void RefreshButton_Click(object sender, EventArgs e)
        {
            this.ShowProcesses();
        }

        
    }
}
